//
//  ZPZBasicView.m
//  Project two framework
//
//  Created by rimi on 16/7/28.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ZPZBasicView.h"
#import "ZZConfig.h"

@interface ZPZBasicView  ()

@end

@implementation ZPZBasicView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = SCREEN_Bounds;
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0.2;
        _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clooseBottomView)];

    }
    return self;
}

- (void)addOtherView
{
    UIWindow *kewWindow = [[UIApplication sharedApplication] keyWindow];
    [kewWindow.rootViewController.view addSubview:self];
    [kewWindow.rootViewController.view addSubview:self.imageView];
    [kewWindow.rootViewController.view addSubview:self.bottomView];
    [self.imageView addSubview:self.closeBtn];
    [self.imageView addSubview:self.nameLabel];
    [self.imageView addSubview:self.shoppingBtn];
    _imageView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    _bottomView.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _imageView.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 0.6;
    } completion:^(BOOL finished) {
        
        if (_bottomView.hidden == NO) {
            [UIView animateWithDuration:0.5 animations:^{
                _bottomView.transform = CGAffineTransformMakeScale(1, 1);
            }];
        }
        
    }];
    
}


#pragma mark - 按钮的实现方法
- (void)closeBtnAction
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.imageView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)clooseBottomView
{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.bottomView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
    } completion:^(BOOL finished) {
        [self.bottomView removeFromSuperview];
    }];
}

#pragma mark -  懒加载
- (UIImageView *)imageView
{
    if (_imageView == nil) {
        _imageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 100, SCREEN_WIDTH - 160, SCREEN_HEIGHT - 200)];
            imageView.image = [UIImage imageNamed:@"底_05.png"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });
    }
    return _imageView;
}

- (UIButton *)closeBtn
{
    if (_closeBtn == nil) {
        _closeBtn = ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:@"close_07.png"] forState:UIControlStateNormal];
            btn.frame = CGRectMake(CGRectGetMaxX(self.imageView.bounds) - 60, 15, 40, 40);
            [btn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _closeBtn;
}


- (UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100 , 30, CGRectGetMaxX(self.imageView.bounds) - 200, 50)];
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont boldSystemFontOfSize:20];
            label;
        });
    }
    return _nameLabel;
}

- (UIView *)bottomView
{
    if (_bottomView == nil) {
        _bottomView = ({
            UIView *view = [[UIView alloc]initWithFrame:self.imageView.bounds];
            [view addGestureRecognizer:_tap];
            view;
        });
    }
    return _bottomView;
}

- (UIButton *)shoppingBtn
{
    if (_shoppingBtn == nil) {
        _shoppingBtn =  ({
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:@"购买.png"] forState:UIControlStateNormal];
            btn.frame = CGRectMake(360, 480, 160, 50);
            [btn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _shoppingBtn;
}

@end
